/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.mvc.controllers
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	public class SliderController extends Controller
	{
		private var _thumb:MovieClip;
		private var _rail:Rectangle;
		public function SliderController(thumb:MovieClip, rail:DisplayObject)
		{
			_thumb = thumb;
			_rail = rail.getRect(rail.parent);
			_rail.x -= _thumb.width / 2;
//			_rail.width -= _thumb.width;
			_rail.y += (_rail.height / 2 - _thumb.height / 2);
			_rail.height = 0;
		}
		
		override protected function setup():void {
		}
		
		override public function beforeReady():void {
			_thumb.addEventListener(MouseEvent.MOUSE_DOWN, startDragHandler);
		}
		
		private function startDragHandler(event:Event):void {
			_thumb.startDrag(false,_rail);
			_thumb.stage.addEventListener(MouseEvent.MOUSE_UP, stopDragHandler);
		}
		
		private function stopDragHandler(event:Event):void {
			_thumb.stopDrag();
			_thumb.stage.removeEventListener(MouseEvent.MOUSE_UP, stopDragHandler);
		}
		
		override public function beforeClose():void {
			_thumb.stage.removeEventListener(MouseEvent.MOUSE_UP, stopDragHandler);
		}
		
		public function set pos(value:Number):void {
			_thumb.x = _rail.x + value * _rail.width / 100;
		}
	}
}